library(apsrtable)
library(sandwich)
library(lmtest)

####
#Relationship between Congruence and Political News Coverage (Contemporary Years Only)
####
load('recent.coverage.RData')

#Cluster SEs at Newspaper Level
fit.w.robust <- function(model, cluster.var, dta){	
	robust.se <- function(model, cluster){
		require(sandwich)
		require(lmtest)
		M <- length(unique(cluster))
		N <- length(cluster)
		K <- model$rank
		dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
		uj <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
		rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
		rcse.se <- coeftest(model, rcse.cov)
		return(list(rcse.cov, rcse.se))
	}
	
	out <- model$model
	clustervar <- mapply(paste,cluster.var,dta[!(1:dim(dta)[1] %in% model$na.action),cluster.var],sep="")
	vcov <- robust.se(model, clustervar)
	model$se <- vcov[[1]]
	model$coeftest <- vcov[[2]]
	return(model)
}

# No. on Reader Share
reader.share.baseline <- lm(count ~ reader.share.in.district,data=recent.coverage)
reader.share.baseline <- fit.w.robust(reader.share.baseline,'paper',recent.coverage)

reader.share.fes <- lm(count ~ reader.share.in.district + factor(paper.cycle),data=recent.coverage)
reader.share.fes <- fit.w.robust(reader.share.fes,'paper',recent.coverage)

# No. on log(Reader Share)
reader.share.baseline.logged <- lm(count ~ log(reader.share.in.district+.001),data=recent.coverage)
reader.share.baseline.logged <- fit.w.robust(reader.share.baseline.logged,'paper',recent.coverage)

reader.share.fes.logged <- lm(count ~ log(reader.share.in.district+.001) + factor(paper.cycle),data=recent.coverage)
reader.share.fes.logged <- fit.w.robust(reader.share.fes.logged,'paper',recent.coverage)

# Article Share on Reader Share
reader.share.baseline.article.share <- lm(article.share ~ reader.share.in.district,data=recent.coverage)
reader.share.baseline.article.share <- fit.w.robust(reader.share.baseline.article.share,'paper',recent.coverage)

reader.share.baseline.fes.article.share <- lm(article.share ~ reader.share.in.district + factor(paper.cycle),data=recent.coverage)
reader.share.baseline.fes.article.share <- fit.w.robust(reader.share.baseline.fes.article.share,'paper',recent.coverage)

apsrtable(reader.share.baseline,reader.share.fes,reader.share.baseline.logged,reader.share.fes.logged,reader.share.baseline.article.share,reader.share.baseline.fes.article.share,omitcoef=expression(grep(pattern="(Intercept)|reader.share",coefnames, invert=TRUE)),coef.names=c('(Intercept)','Reader Share','Log(Reader Share)'),model.names=c('# Articles','w/ Newspaper FEs','# Articles','w/ Newspaper FEs','Share of Articles','w/ Newspaper FEs'),notes=list(se.note='Robust standard errors, clustered by Newspaper, in parentheses',stars.note=stars.note))

####
#Relationship between Congruence and Political News Coverage (Over Time Comparison to S&S Newspapers)
####

load('overtime.coverage.RData')

post.model <- lm(count ~ reader.share,data=overtime.coverage[which(overtime.coverage$timing=='post'),])
post.model <- fit.w.robust(post.model,'paper',overtime.coverage[which(overtime.coverage$timing=='post'),])

pre.model <- lm(count ~ reader.share,data=overtime.coverage[which(overtime.coverage$timing=='pre'),])
pre.model <- fit.w.robust(pre.model,'paper',overtime.coverage[which(overtime.coverage$timing=='pre'),])

post.model.fe <- lm(count ~ reader.share + factor(paper) + factor(cycle),data=overtime.coverage[which(overtime.coverage$timing=='post'),])
post.model.fe <- fit.w.robust(post.model.fe,'paper',overtime.coverage[which(overtime.coverage$timing=='post'),])

pre.model.fe <- lm(count ~ reader.share + factor(paper) + factor(cycle),data=overtime.coverage[which(overtime.coverage$timing=='pre'),])
pre.model.fe <- fit.w.robust(pre.model.fe,'paper',overtime.coverage[which(overtime.coverage$timing=='pre'),])

apsrtable(pre.model,pre.model.fe,post.model,post.model.fe,model.names=c('1991-2002','1991-2002','2005-2016','2005-2016'),se=c('robust'),caption='Congressional District Reader Share and News Coverage of Representative',omitcoef=expression(grep(pattern="(Intercept)|reader.share",coefnames, invert=TRUE)),coef.names=c('(Intercept)','Reader Share'))

####
# District-Level Relationship between Congruence and Political News Coverage (Over Time Comparison to S&S Newspapers)
####

load('district.coverage.RData')
pre.district.level <- lm(weighted.coverage ~ congruence + factor(cycle),data=district.coverage[which(district.coverage$timing=='pre'),])
post.district.level <- lm(weighted.coverage ~ congruence + factor(cycle),data=district.coverage[which(district.coverage$timing=='post'),])

apsrtable(pre.district.level,post.district.level,model.names=c('1991-2002','2005-2016'),caption='Congressional District Congruence and Market-Share Weighted Coverage of Representative',omitcoef=expression(grep(pattern="(Intercept)|congruence",coefnames, invert=TRUE)),coef.names=c('(Intercept)','Congruence'))